/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.qdsg.ysg.doctor.ui.dialog;

import android.app.Dialog;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.qdsg.ysg.doctor.util.DpUtil;
import com.qdsg.ysg.doctor.util.SoftKeyboardUtil;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.response.DoctorResponse;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseDialogFragment;


public class TeamDoctorDialog extends BaseDialogFragment {
    private OnItemClickListener mListener;
    private Dialog mDialog;
    private RecyclerView recyclerView;
    private EditText edt_search;
    MyAdapter adapter;
    List<DoctorResponse.Doctor> list = new ArrayList<>();
    int type;

    public static TeamDoctorDialog newInstance() {
        TeamDoctorDialog itemDialog = new TeamDoctorDialog();
        Bundle bundle = new Bundle();
        itemDialog.setArguments(bundle);
        return itemDialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    @NonNull
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDialog = super.onCreateDialog(savedInstanceState);
        mDialog.setCancelable(false);
        Window window = mDialog.getWindow();
        if (window != null) {
            window.setGravity(Gravity.CENTER);
        }
        return mDialog;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.dialog_item2, container, false);
        mView.setMinimumWidth(mScreenWidth);
        type = getArguments().getInt("type");
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView = mView.findViewById(R.id.recyclerView);
        edt_search = mView.findViewById(R.id.edt_search);
        recyclerView.setLayoutManager(manager);
        adapter = new MyAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.bottom = DpUtil.dip2px(getActivity(), 1);
            }
        });


        edt_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    // 按下完成按钮，这里和上面imeOptions对应
                    getDoctorByName(edt_search.getText().toString());
                    SoftKeyboardUtil.hideSoftKeyboard(getActivity());
                }
                return false;
            }
        });



        return mView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getDialog().setCanceledOnTouchOutside(false);
    }

    public interface OnItemClickListener {
        void onItem(DoctorResponse.Doctor doctor);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }


    public boolean isShowing() {
        return mDialog != null && mDialog.isShowing();
    }


    class MyAdapter extends RecyclerView.Adapter<MyAdapter.Holder> {

        @Override
        public MyAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyAdapter.Holder myHolder;
            myHolder = new MyAdapter.Holder(LayoutInflater.from(getActivity()).inflate(R.layout.item_team_doctor, parent, false));
            return myHolder;
        }

        @Override
        public void onBindViewHolder(MyAdapter.Holder holder, final int position) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItem(list.get(position));
                }
            });
            holder.tv_name.setText(list.get(position).docName);
            holder.tv_job.setText(list.get(position).jobTitleName);
            if (list.get(position).memberType == 0) {
                holder.tv_type.setText("成员");
                holder.btn_del.setVisibility(View.GONE);
            }else{
                holder.tv_type.setText("负责人");
                holder.btn_del.setVisibility(View.GONE);
            }

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class Holder extends RecyclerView.ViewHolder {

            TextView tv_name, tv_job, tv_type;
            ImageView btn_del;

            public Holder(View itemView) {
                super(itemView);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_job = itemView.findViewById(R.id.tv_job);
                tv_type = itemView.findViewById(R.id.tv_type);
                btn_del = itemView.findViewById(R.id.btn_del);
            }
        }
    }

    private void getDoctorByName(String name) {
        RestProxy.getInstance().getDoctorByName(name,new Observer<DoctorResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(DoctorResponse doctorResponse) {
                    list.clear();
                    list.addAll(doctorResponse.data);
                    adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError(getActivity(), e);
            }

            @Override
            public void onComplete() {

            }
        });
    }

}
